<?php

/**
 * This is the model class for table "page_url".
 */
require_once(dirname(__FILE__).'/base/PageUrlBase.php');
class PageUrl extends PageUrlBase
{
    const STATUS_DEACTIVE = 0, STATUS_ACTIVE = 1;
    
    public function behaviors()
    {
        return array(
            'timestamp'=>array(
                'class'=>'TimestampBehavior',
            ),
        );
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('url', 'required', 'on'=>'new'),
            array('url', 'validUrl'),
            array('url', 'uniqueUrl'),
            array('page_id, status, default, creation_datetime, last_update', 'safe'),
            array('id, page_id, url, status, default, creation_datetime, last_update', 'safe', 'on'=>'search'),
        );
    }
    
    public function validUrl($attribute, $params)
    {
        if (empty($this->{$attribute}) == false)
        {
            $url = $this->{$attribute};
            if (strpos($url, 'http://') === false)
            {
                $url = 'http://'.$url;
            }
            $url = str_replace('.html', '', $url);
            $validate = new CUrlValidator();
            if ($validate->validateValue($url))
            {
                $this->addError($attribute, Yii::t('application', 'Url "{value}" don\'t include hostname.', array('{value}'=>$this->{$attribute})));
                return false;
            }
            $this->{$attribute} = '/'.trim($this->{$attribute}, '/');
        }
        return true;
    }
    
    public function uniqueUrl($attribute, $params)
    {
        if (empty($this->{$attribute})) return true;
        $criteria = new CDbCriteria();
        $criteria->join = 'INNER JOIN '.SITE_TABLE_PREFIX.'page p ON p.id = t.page_id';
        $page = $this->page;
        if (is_object($page))
            $siteId = $page->site_id;
        else
        {
            $siteId = Yii::app()->cmsManager->site['id'];
        }
        $criteria->compare('p.site_id', $siteId);
        if ($this->id)
            $criteria->compare('"t".id', '<>'.$this->id);
        $criteriaSub = new CDbCriteria();
        $criteriaSub->compare('url', $this->{$attribute});
        $criteriaSub->compare('url', trim($this->{$attribute}, '/'), false, 'OR');
        $criteria->mergeWith($criteriaSub);
        if (PageUrl::model()->exists($criteria))
        {
            $this->addError($attribute, Yii::t('application', 'Url "{value}" has already been taken.', array('{value}'=>$this->{$attribute})));
            return false;
        }
        return true;
    }
    
    public function getStatusOptions()
    {
        return array(
            self::STATUS_DEACTIVE => 'Deactive',
            self::STATUS_ACTIVE => 'Active',
        );
    }
    
    public function getStatusText()
    {
        $options = $this->getStatusOptions();
        return isset($options[$this->status]) ? $options[$this->status] : Yii::t('app', 'Unknown {att}', array('{att}'=>$this->status));
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        $criteria=new CDbCriteria;

        $criteria->compare('page_id',$this->page_id);
        $criteria->compare('url',$this->url,true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'id'
            ),
            'pagination'=>array(
                'pageSize'=>Settings::BO_PAGE_SIZE,
            ),
        ));
    }
}